@file:Suppress("NOTHING_TO_INLINE")

package org.glavo.bot

import net.mamoe.mirai.*
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.MiraiExperimentalApi
import org.glavo.bot.data.Config
import org.glavo.bot.data.Data

lateinit var bot: Bot
lateinit var MainGroup: Group
lateinit var CommandGroup: Group
var botStopped: Boolean = false

@MiraiExperimentalApi
suspend fun main() {
    System.setProperty("mirai.no-desktop", "true")

    bot = BotFactory.newBot(Config.qq, Config.password) {
        protocol = BotConfiguration.MiraiProtocol.ANDROID_PAD
        loadDeviceInfoJson(Data.readResourceAsText("secret/Device.json"))
    }
    bot.login()

    MainGroup = bot.getGroup(Config.MainGroupID)!!
    CommandGroup = bot.getGroup(Config.CommandGroupID)!!

    if (Config.isOnMCServer) {
        monitorServer()
    } else {
        bot.logger.info("未运行在 Minecraft 服务器上")
    }

    ddns()
    //checkPages()

    bot.eventChannel.subscribeAlways<MessageEvent> { event ->
        if ((event is GroupMessageEvent && (event.group.id == Config.MainGroupID || event.group.id == Config.CommandGroupID))
            || event.sender.id == 360736041L
        ) {
            val qr = message[QuoteReply]
            val m = message

            /*
            if (event is GroupMessageEvent) {
                if (qr == null) {
                    message
                } else {
                    val tem = arrayListOf<Message>()
                    var rmAt = true
                    var rmSpace = false

                    for (m in message) {
                        when (m) {
                            is At -> {
                                if (rmAt && m.target == qr.source.fromId) {
                                    rmAt = false
                                    rmSpace = true
                                } else {
                                    tem += m
                                }
                            }
                            is PlainText -> {
                                if (m.content.isNotEmpty()) {
                                    if (rmSpace) {
                                        if (m.content.startsWith(' ')) {
                                            if (m.content.length > 1) {
                                                tem += PlainText(m.content.substring(1))
                                            }
                                        } else {
                                            tem += m
                                        }
                                        rmSpace = false
                                    } else {
                                        tem += m
                                    }
                                }
                            }
                            else -> tem += m
                        }
                    }
                    tem.toMessageChain()
                }
            } else {
                message
            }
             */
            val c = m.content

            val command = Command.All.firstOrNull { it.invoke(event, m, c) }

            if (command == null) {
                if (c.startsWith('#') && qr == null) {
                    event.subject.sendMessage(event.message.quote() + "未知命令")
                }
            } else if (command != Command.Forward && !botStopped) {
                if (event.sender.id !in ChatOffList && (event as? GroupMessageEvent)?.group == MainGroup)
                    forward(event, m)
            }
        }
    }

    /*
    bot.eventChannel.subscribeAlways<MemberJoinEvent> { event ->
        if (event.group == MainGroup) {
            group.sendMessage(PlainText("欢迎") + event.member.at() + "加入！关于服务器的更多信息，请查看服务器公告： ${Config.Post}")
        }
    }
     */
    bot.join()
}
